package com.lucweb.dao.impl;


import com.lucweb.dao.TodayDao;
import com.lucweb.entity.TaskToday;

import org.hibernate.SessionFactory;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Transactional(rollbackFor = Exception.class)
@Repository("todayDao")
public class TodayDaoImpl implements TodayDao {


    @Resource(name="sessionFactory")
    private SessionFactory sessionFactory;


    @Override
    public List<TaskToday> todayList(String para) {
        List<TaskToday> todayList = (List<TaskToday>) sessionFactory.getCurrentSession().createQuery("FROM TaskToday where companyName ='"+para+"'").list();

        return todayList;
    }

    @Override
    public void saveOrUpdate(TaskToday tasktoday) {
        sessionFactory.getCurrentSession().saveOrUpdate(tasktoday);

    }

    @Override
    public TaskToday gettodayById(int taskId) {
        return (TaskToday) sessionFactory.getCurrentSession().get(TaskToday.class, taskId);
    }

    @Override
    public void deletetoday(int taskId) {

        sessionFactory.getCurrentSession().delete(
                sessionFactory.getCurrentSession().get(TaskToday.class, taskId)
        );
    }

    @Override
    public List<TaskToday> showtoday() {
        List<TaskToday> todayList = (List<TaskToday>) sessionFactory.getCurrentSession().createQuery("FROM TaskToday ").list();

        return todayList;
    }

    @Override
    public int getmaxId() {
        Integer number = (Integer) sessionFactory.getCurrentSession().createSQLQuery("select auto_increment from " +
                "information_schema.tables where table_schema='firesystem' and table_name='task_today';")
                .addScalar("auto_increment", StandardBasicTypes.INTEGER).uniqueResult();
        return number;
    }

    @Override
    public List<String> taskShow() {


        List<String> taskList=sessionFactory.getCurrentSession().createSQLQuery("SELECT executeTime FROM task_whyj\n" +
                "UNION SELECT executeTime FROM task_jc\n" +
                "UNION SELECT executeTime FROM task_wx\n" +
                "ORDER BY executeTime").addScalar("executeTime",StandardBasicTypes.STRING).list();

        return taskList;
    }
}
